package com.hougong.im.utils;

import com.hougong.im.common.Constants;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Date;

public class FileUtils {
    private FileUtils() {

    }

    /**
     * 保存头像
     *
     * @param username
     * @param file
     * @return 返回虚拟地址 /upload/xxxxx
     * @throws IOException
     */
    public static String uploadFile(String username, MultipartFile file) throws IOException {
        String fileName = username + "_" + DateUtils.format(new Date(), "yyyyMMddHHmmss") + "_" + file.getOriginalFilename();
        File dir = new File(Constants.Upload_Path_Dir, fileName);
        if (!dir.getParentFile().exists()) {
            dir.getParentFile().mkdirs();
        }
        file.transferTo(dir);
        String virtualPath = Constants.virtual_Path_Dir + fileName;
        return virtualPath;
    }

    /**
     * 计算图片md5,作为文件名字，这样当发送同一张图片的时候只需要保存一份就可以了
     * 返回虚拟文件路径
     *
     * @param username
     * @param file
     * @return
     * @throws IOException
     */
    public static String uploadPic(String username, MultipartFile file) throws IOException {
//        String fileName = username+"_"+DateUtils.format(new Date(),"yyyyMMddHHmmss") +"_"+file.getOriginalFilename();
//        File disFile = new File(Constants.Upload_Path_Dir,fileName);
//        if(!disFile.getParentFile().exists()){
//            disFile.getParentFile().mkdirs();
//        }
//        InputStream inputStream = file.getInputStream();
//
//        Thumbnails.of(inputStream).size(768,1024).toFile(disFile);
//        if(inputStream!=null){
//            inputStream.close();
//        }
//        String virtualPath=Constants.virtual_Path_Dir+fileName;
//        return virtualPath;

        InputStream inputStream = file.getInputStream();
        BufferedInputStream bis=new BufferedInputStream(inputStream);
        bis.mark(inputStream.available()+1);
        String md5 = DigestUtils.md5Hex(bis);
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        String distFileName = md5 + "." + suffix;
        File existFile = new File(Constants.Upload_Path_Dir, distFileName);
        if (!existFile.exists()) {
            bis.reset();
            org.apache.commons.io.FileUtils.copyInputStreamToFile(bis, existFile);
        }
        return Constants.virtual_Path_Dir + distFileName;
    }

    public static void main(String[] args) throws IOException {
        String a = "C:\\Users\\Administrator\\Pictures\\a.jpg";
        String b = "C:\\Users\\Administrator\\Pictures\\a - 副本.jpg";
        File file = new File(a);
        FileInputStream fis = new FileInputStream(file);
        String s = DigestUtils.md5Hex(fis);
        System.out.println("a图片md5:" + s);
        fis.close();
        File file2 = new File(b);
        FileInputStream fis2 = new FileInputStream(file2);
        String s2 = DigestUtils.md5Hex(fis2);
        System.out.println("b图片md5:" + s2);
        fis2.close();
    }
}
